// const { cloud } = require("../../utils/cloud");
import { cloud } from "../../utils/cloud"

// pages/index/index.js
Page({
  data: {
    unLoginAvatar: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132",
    menuShow: false,
    menuHide: false,
    showAddTodo: false, //是否显示添加信息组件
    list: [],//存放用户的代办事项清单
    showTask: false,
    todos: []
  },
  onLoad(options) {
    this.getList()
  },
  getTodoCheck(e){//更新完成todo任务
    cloud({
      url:"task/do",
      data:{
        _id:e.currentTarget.dataset.id,
        checked:e.detail.value
      }
    }).then(res=>{
      console.log(res);
    }).catch(e=>{})
  },
  goDetail(e){//跳转到任务详情编辑页面
    wx.navigateTo({
      url: '/pages/todo-detail/index?id='+e.currentTarget.dataset.id,
    })
  },
  getList() {
    //发起网络请求获取任务清单信息
    cloud({ url: "list" }).then(res => {
      this.setData({
        list: res.data
      });
    }).catch(e => { })
    // 获取任务清单
    cloud({ url: 'task/get' }).then(res => {
      this.setData({
        todos: res.data
      })
    }).catch(e => { })
  },
  addList() {
    // 控制组件显示
    this.setData({
      showTask: true
    })
  },
  getTaskValue(e) {
    this.setData({
      showTask: false
    });
    if (!e.detail.value) {
      wx.showToast({
        title: '请输入内容',
        icon: 'error',
      })
      return;
    }
    cloud({
      url: "list/add",
      data: {
        color: e.detail.color,
        name: e.detail.value,
      }
    }).then(res => {
      wx.showToast({//处理正确数据
        title: '添加成功'
      })
      this.getList();//重新获取一下最新的清单列表
    }).catch(e => { })//处理错误数据
  },
  onReady() {

  },
  onShow() {

  },
  showMenu() { //展示菜单栏
    this.setData({
      menuShow: true
    })
  },
  closeMenu() { //关闭菜单栏
    this.setData({
      menuHide: true
    });
    setTimeout(() => {
      this.setData({
        menuHide: false,
        menuShow: false,

      })
    }, 550)
  },
  openAdd() {
    this.setData({
      showAddTodo: true
    })
  },
  getTaskData(e) {//添加任务
    this.setData({
      showAddTodo:false
    });
    cloud({
      url: "task/add",
      data: {
        alarm: e.detail.alarm,
        time: e.detail.date,
        list_id: e.detail.listId,
        order: e.detail.order,
        name: e.detail.value
      }
    }).then(res=>{
      if(res._id){
        this.getList()
      }
    }).catch(e=>{})
  },
  onHide() {

  },
  onUnload() {
  },
  onPullDownRefresh() {
  },
  onReachBottom() {

  },
  onShareAppMessage() {

  }
})